<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Download extends MY_Controller {
   
	public function __construct(){
		parent::__construct();
		$this->load->model('users_m', 'users');		
		$userdata = $this->session->userdata('userdata');
		if(!$userdata['ntd_id'])
			show_404();
	}
	
	function index(){
		echo 'Ajax fail';
	}
	
	function profile(){
		$id	=	$this->input->post('id');
		$user	=	$this->session->userdata('userdata');
		if(!$id || !$user){
			echo json_encode(array(
				'status'	=>	'fail',
				'message'=>	'Có lỗi xảy ra. Vui lòng thử lại sau'
			));
			die;
		}
		$this->load->model('tuyendung_m', 'tuyendung');			
		$point	=	$this->config->item('point_per_download_profile');
		$row = false;
		$this->load->model('canhan_m', 'canhan');
		$obj = $this->canhan->get_canhan($id, true);
		$this->load->model('message_m', 'message');
		$message	=	$this->message->get_message_by_slug('download_profile_content');
		$content = $message->content;
		$href = '<a target="_blank" href="'.site_url('pg-pb-model/'.remove_accents($obj->ho_ten).'-'.$obj->id).'">'. $obj->ho_ten.'</a>';
		$content = str_replace("{profile}", $href, $content);
		$content = str_replace("{point}", $point, $content);
		$row	=	$this->tuyendung->get_phone_catse($user['id'], $id, $point, $content, 1);		
		if($row){			
			$this->load->library('pdf');
			$data = array();			
			$this->load->model('album_m', 'album');
			$data['obj']	= $obj;
			$data['album']	= $this->album->get_album_by_canhan_id($obj->id);
			$uri = site_url('pg-pb-model/'.remove_accents($obj->ho_ten).'-'.$obj->id);
			// create new PDF document
			$mpdf = new Pdf('utf-8','A4','','',15,15,40,30,10,10);
			// $mpdf->mirrorMargins = 1;
			$header = '<table width="100%" style="border-bottom: 1px solid #000000; vertical-align: bottom; font-family: tahoma; font-size: 9pt; color: #000088;">';
			$header .= '<tr>';
			$header .= '<td width="33%"><img width="200" src="/assets/img/logo-pg-vietnam.png"/></td>';
			$header .= '<td width="33%" align="center"></td>';
			$header .= '<td width="33%" style="text-align: right;">';
			$header .= '<a href="'.$uri.'" style="text-decoration:none;">'.$uri.'</a>';
			$header .= '</td>';
			$header .= '</tr></table>';
			$footer = '<div align="center"><a href="http://pgvietnamworks.com">pgvietnamworks.com</a></div>';
			$mpdf->SetHTMLHeader($header);
			$mpdf->SetHTMLFooter($footer);
			$this->load->view('tpl/profile', $data);
			$html = $this->output->get_output();
			$path_tbao = '';
			$mpdf->useAdobeCJK = true;
			$stylesheet = file_get_contents('assets/css/mpdfstyleA4.css');
			$mpdf->WriteHTML($stylesheet,1);
			$mpdf->SetAutoFont(AUTOFONT_ALL);		
			$mpdf->WriteHTML($html);
			$str = $mpdf->Output('', 'S');
			header('Set-Cookie: fileDownload=true; path=/');
			header('Cache-Control: max-age=60, must-revalidate');
			header('Content-Type: application/octet-stream');
			header('Content-Length: '.strlen($mpdf->buffer));
			header('Content-Disposition: filename='.remove_accents($obj->ho_ten).'.pdf');
			echo $str;
		}else
			echo 'Không đủ tiền trong tài khoản';
		die;
	}
	
}
